<?php

namespace app\api\service;

use app\common\model\circle\CircleFansModel;
use app\common\model\dynamic\DynamicModel;
use app\common\model\user\UserModel;

class CircleService
{
    public static function circle_find()
    {
        return ['id,name,avatar,intro,browse'];
    }

    public static function circle_map($data)
    {
        $todayTime = strtotime('today 00:00:00');
        $data->map(function ($v) use ($todayTime) {
            $v['is_hot'] = DynamicModel::where('circle_id', $v['id'])->where('create_time', '>', $todayTime)->value('id') > 0;
            $v['is_new'] = CircleFansModel::where('circle_id', $v['id'])->where('create_time', '>', $todayTime)->value('id') > 0;
            $v['dynamic_count'] = DynamicModel::where('circle_id', $v['id'])->count();
            $v['user_count'] = CircleFansModel::where('circle_id', $v['id'])->count();
            if ($v['user_count'] > 0) {
                $uids = CircleFansModel::where('circle_id', $v['id'])->orderRaw('RAND()')->limit(3)->column('user_id');
                $v['user'] = UserModel::whereIn('id', $uids)->column('avatar');
            }
            return $v;
        });
        return $data;
    }
}
